package com.kede.app.rest;

import com.kede.annotation.Log;
import com.kede.app.domain.Supplies;
import com.kede.app.service.SuppliesService;
import com.kede.app.service.dto.SuppliesQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author Pr.li
* @date 2023-06-06
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "supplie管理")
@RequestMapping("/api/supplies")
public class SuppliesController {

    private final SuppliesService suppliesService;

    /**
     * 导出数据
     **/
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('supplies:list')")
    public void exportSupplies(HttpServletResponse response, SuppliesQueryCriteria criteria) throws IOException {
        suppliesService.download(suppliesService.queryAll(criteria), response);
    }

    /**
    * 查询supplie
    **/
    @GetMapping
    @Log("查询supplie")
    @ApiOperation("查询supplie")
    @PreAuthorize("@el.check('supplies:list')")
    public ResponseEntity<Object> querySupplies(SuppliesQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(suppliesService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    /**
    * 新增supplie
    **/
    @PostMapping
    @Log("新增supplie")
    @ApiOperation("新增supplie")
    @PreAuthorize("@el.check('supplies:add')")
    public ResponseEntity<Object> createSupplies(@Validated @RequestBody Supplies resources){
        return new ResponseEntity<>(suppliesService.create(resources),HttpStatus.CREATED);
    }


    /**
    * 修改supplie
    **/
    @PutMapping
    @Log("修改supplie")
    @ApiOperation("修改supplie")
    @PreAuthorize("@el.check('supplies:edit')")
    public ResponseEntity<Object> updateSupplies(@Validated @RequestBody Supplies resources){
        suppliesService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


    /**
    * 删除supplie
    **/
    @DeleteMapping
    @Log("删除supplie")
    @ApiOperation("删除supplie")
    @PreAuthorize("@el.check('supplies:del')")
    public ResponseEntity<Object> deleteSupplies(@RequestBody Integer[] ids) {
        suppliesService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}